package com.jcj.pojomessagepack;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;

/**
 * 客户端handler初始化配置类,在Channel注册到EventLoop后,对这个Channel添加一些初始化的Handler
 */
public class InitializerClientPM extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();
        //自定义长度解码器:LengthFieldBasedFrameDecoder
        //因为长度域是放在消息序列的头部,这里去除头部长度域,指定参数:initialBytesToStrip:2
        pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2));
        pipeline.addLast("MessagePack decoder", new MessagePackDecoder());
        pipeline.addLast("prepender", new LengthFieldPrepender(2));//长度域两个字节
        pipeline.addLast("MessagePack encoder", new MessagePackEncoder());
        //自定义业务逻辑handler
        pipeline.addLast(new HandlerClientPM());

    }
}




















